package study.transaction.jdbc.dao.userbase;

import lombok.SneakyThrows;
import study.transaction.jdbc.dao.BaseDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

public class UserBaseDao extends BaseDao {

    public UserBaseDao(Connection connection) {
        super(connection);
    }

    @SneakyThrows
    public int insert(UserBaseEO entity) {
        //
        String sql = "insert into user_base (ub_login_name) value (?) ";
        //
        PreparedStatement pstmt = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
        //绑定参数
        pstmt.setString(1, entity.getUbLoginName());
        //执行sql
        int size = pstmt.executeUpdate();

        //自增主键
        ResultSet generatedKeys = pstmt.getGeneratedKeys();
        while (generatedKeys.next()) {
            Long ubId = generatedKeys.getLong(1);
            entity.setUbId(ubId);
        }

        return size;
    }
}
